﻿Public Class STOCKCLASS
    Dim connect As CONDB = CONDB.NewConnection()

    Private STKID = Nothing
    Private STKCODE = Nothing
    Private STKNAME = Nothing
    Private LOCATION = Nothing
    Private F_SALE = Nothing
    Private F_DRUG = Nothing
    Private F_MEDSUP = Nothing
    Private F_SUPPLY = Nothing
    Private STATUS = Nothing

    Private selectStock = "SELECT stkid, stkcode, stkname, location, f_sale, f_drug, f_medsup, f_supply, STATUS FROM stock "

    Property selectStock_ As String
        Get
            Return selectStock
        End Get
        Set(value As String)
            selectStock = value
        End Set
    End Property

    Property STKID_ As Integer
        Get
            Return STKID
        End Get
        Set(value As Integer)
            STKID = value
        End Set
    End Property

    Property STKCODE_ As String
        Get
            Return STKCODE
        End Get
        Set(value As String)
            STKCODE = value
        End Set
    End Property

    Property STKNAME_ As String
        Get
            Return STKNAME
        End Get
        Set(value As String)
            STKNAME = value
        End Set
    End Property

    Property LOCATION_ As String
        Get
            Return LOCATION
        End Get
        Set(value As String)
            LOCATION = value
        End Set
    End Property

    Property F_SALE_ As Boolean
        Get
            Return F_SALE
        End Get
        Set(value As Boolean)
            If value = True Then
                F_SALE = 1
            Else
                F_SALE = 0
            End If
        End Set
    End Property

    Property F_DRUG_ As Boolean
        Get
            Return F_DRUG
        End Get
        Set(value As Boolean)
            If value = True Then
                F_DRUG = 1
            Else
                F_DRUG = 0
            End If
        End Set
    End Property

    Property F_MEDSUP_ As Boolean
        Get
            Return F_MEDSUP
        End Get
        Set(value As Boolean)
            If value = True Then
                F_MEDSUP = 1
            Else
                F_MEDSUP = 0
            End If
        End Set
    End Property

    Property F_SUPPLY_ As Boolean
        Get
            Return F_SUPPLY
        End Get
        Set(value As Boolean)
            If value = True Then
                F_SUPPLY = 1
            Else
                F_SUPPLY = 0
            End If
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property


    Public Function insertStock() As Boolean
        If connect.GetTable("SELECT stkid FROM stock WHERE stkcode = '" & STKCODE & "' OR stkname = '" & STKNAME & "'").Rows.Count > 0 Then
            MsgBox("ไม่สามารถเพิ่มรายการ stock ดังกล่าวได้ เนื่องจากมีรายการดังกล่าวอยู่แล้ว")
            Return False
        Else
            Try
                connect.BeginTrans()

                Dim sql1 As String = ""
                Dim sql2 As String = ""
                Dim sql3 As String = ""

                If STKCODE IsNot Nothing Then
                    sql2 += "stkcode, "
                    sql3 += "'" & STKCODE & "', "
                Else
                    sql2 += "stkcode, "
                    sql3 += "default, "
                End If

                If STKNAME IsNot Nothing Then
                    sql2 += "stkname, "
                    sql3 += "'" & STKNAME & "', "
                Else
                    sql2 += "stkname, "
                    sql3 += "default, "
                End If

                If LOCATION IsNot Nothing Then
                    sql2 += "location, "
                    sql3 += "'" & LOCATION & "', "
                Else
                    sql2 += "location, "
                    sql3 += "default, "
                End If

                sql2 += "f_sale, "
                sql3 += "'" & F_SALE & "', "

                sql2 += "f_drug, "
                sql3 += "'" & F_DRUG & "', "

                sql2 += "f_medsup, "
                sql3 += "'" & F_MEDSUP & "', "

                sql2 += "f_supply, "
                sql3 += "'" & F_SUPPLY & "', "

                sql2 += "status "
                sql3 += "'" & STATUS & "' "

                sql1 += "INSERT INTO stock (" & sql2 & ") VALUES (" & sql3 & ");"

                connect.ExecuteNonQuery(sql1)
                connect.CommitTrans()
                MsgBox("เพิ่มข้อมูล stock สำเร็จแล้ว")
                Return True
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
                Return False
            Finally
                connect.Dispose()
            End Try
        End If
    End Function

    Public Function editStock() As Boolean
        Try
            connect.BeginTrans()

            Dim sql As String = ""
            sql += "UPDATE stock SET "

            sql += "stkcode = '" & STKCODE & "', "
            sql += "stkname = '" & STKNAME & "', "
            sql += "location = '" & LOCATION & "', "
            sql += "f_sale = '" & F_SALE & "', "
            sql += "f_drug = '" & F_DRUG & "', "
            sql += "f_medsup = '" & F_MEDSUP & "', "
            sql += "f_supply = '" & F_SUPPLY & "', "
            sql += "status = '" & STATUS & "' "
            sql += "WHERE stkid = '" & STKID & "'"

            connect.ExecuteNonQuery(sql)
            connect.CommitTrans()
            MsgBox("แก้ไขข้อมูล stock สำเร็จแล้ว")
            Return True
        Catch ex As Exception
            connect.RollbackTrans()
            MsgBox("ไม่สามารถแก้ไขข้อมูลได้" & ex.ToString)
            Return False
        Finally
            connect.Dispose()
        End Try
    End Function
End Class
